package com.ruoyi.factory.matecount.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.page.TableDataInfo;
import net.sf.json.JSONObject;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factory.matecount.domain.MaterialCount;
import com.ruoyi.factory.matecount.service.IMaterialCountService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 物料销售数据Controller
 * 
 * @author ljj
 * @date 2024-03-06
 */
@EnableScheduling
@RestController("materialCountController")
@RequestMapping("/system/count")
public class MaterialCountController extends BaseController {
    @Autowired
    private IMaterialCountService materialCountService;

    /**
     * 查询物料销售数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:count:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialCount materialCount) {
        List<MaterialCount> list = materialCountService.selectMaterialCountList(materialCount);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:count:list')")
    @GetMapping("/getroot")
    public TableDataInfo getroot(MaterialCount materialCount) {
        startPage();
        if ("1".equals(materialCount.getMateType()) || "2".equals(materialCount.getMateType())) {
            materialCount.setParentId("");
        }else{
            materialCount.setMateType("0");
        }
        List<MaterialCount> list = materialCountService.selectMaterialCountRootList(materialCount);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:count:list')")
    @GetMapping("/getchildren")
    public AjaxResult getchildren(MaterialCount materialCount) {
        MaterialCount materialCount2 = new MaterialCount();
        MaterialCount materialCount1 = materialCountService.selectMaterialCountById(materialCount.getId());
//        materialCount2.setRootCode(materialCount1.getRootCode());
        materialCount2.setBomVar(materialCount1.getBomVar());
        materialCount2.setParentId(materialCount1.getPartId());
//        materialCount2.setSeqnum(materialCount1.getSeqnum());
        materialCount2.setBomName(materialCount1.getBomName());
        materialCount2.setPeriodDate(materialCount1.getPeriodDate());
        //查询出该父节点下的字节点
        List<MaterialCount> list = materialCountService.selectMaterialCountRootList(materialCount2);
        return AjaxResult.success(list);
    }

    /**
     * 导出物料销售数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:count:export')")
    @Log(title = "物料销售数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialCount materialCount) {
        List<MaterialCount> list = materialCountService.selectMaterialCountList(materialCount);
        ExcelUtil<MaterialCount> util = new ExcelUtil<MaterialCount>(MaterialCount.class);
        util.exportExcel(response, list, "物料销售数据数据");
    }

    /**
     * 获取物料销售数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:count:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(materialCountService.selectMaterialCountById(id));
    }

    /**
     * 新增物料销售数据
     */
    @PreAuthorize("@ss.hasPermi('system:count:add')")
    @Log(title = "物料销售数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialCount materialCount) {
        return toAjax(materialCountService.insertMaterialCount(materialCount));
    }

    /**
     * 修改物料销售数据
     */
    @PreAuthorize("@ss.hasPermi('system:count:edit')")
    @Log(title = "物料销售数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialCount materialCount) {
        return toAjax(materialCountService.updateMaterialCount(materialCount));
    }

    /**
     * 删除物料销售数据
     */
    @PreAuthorize("@ss.hasPermi('system:count:remove')")
    @Log(title = "物料销售数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(materialCountService.deleteMaterialCountByIds(ids));
    }

    /**
     * @author: Liujiaji
     * @date: 2024/3/7 09:01
     * @description: TODO 更新erp数据到中间服务数据库  规则:1.查询erp销售物料 2.根据销售物料查询本地数据统计 有则累计没有则 查询bom 有则处理 没有则标记为其他
     * @param: [type]
     * @return: void
     **/
    @PostMapping("/updatame")
    public void updatame(@RequestBody String req) {
        JSONObject jsonObject = JSONObject.fromObject(req);
        materialCountService.updatame(jsonObject);
    }


//    public static void main(String[] args) {
//        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//        String dbURL = "jdbc:sqlserver://192.168.0.251:1433;DatabaseName=RF0727";
//        String userName = "richfuljc";
//        String userPwd = "richful@JC";
//        Connection dbConn = null;
//        try {
//            // 注册驱动
//            Class.forName(driverName);
//            // 获取数据库连接
//            dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
//            System.out.println("连接数据库成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.print("连接失败");
//        }
//
//    }
}


